package com.ruoyi.web.controller.car;

import java.util.List;

import com.ruoyi.system.domain.Cheyouhui;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ICheyouService;
import com.ruoyi.system.service.ICheyouhuiService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Cheyou;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 车友会人员 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-07-16
 */
@Controller
@RequestMapping("/system/cheyou")
public class CheyouController extends BaseController
{
    private String prefix = "system/cheyou";
	
	@Autowired
	private ICheyouService cheyouService;
	@Autowired
	private ICheyouhuiService cheyouhuiService;
	
	@RequiresPermissions("system:cheyou:view")
	@GetMapping()
	public String cheyou()
	{
	    return prefix + "/cheyou";
	}
	
	/**
	 * 查询车友会人员列表
	 */
	@RequiresPermissions("system:cheyou:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Cheyou cheyou)
	{
		startPage();
        List<Cheyou> list = cheyouService.selectCheyouList(cheyou);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出车友会人员列表
	 */
	@RequiresPermissions("system:cheyou:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Cheyou cheyou)
    {
    	List<Cheyou> list = cheyouService.selectCheyouList(cheyou);
        ExcelUtil<Cheyou> util = new ExcelUtil<Cheyou>(Cheyou.class);
        return util.exportExcel(list, "cheyou");
    }
	
	/**
	 * 新增车友会人员
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		mmap.put("cheyous",cheyouhuiService.selectCheyouhuiList(null));
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存车友会人员
	 */
	@RequiresPermissions("system:cheyou:add")
	@Log(title = "车友会人员", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Cheyou cheyou)
	{
		Cheyouhui cheyouhui = cheyouhuiService.selectCheyouhuiById(cheyou.getCheyouhuiId());
		List<Cheyou> list = cheyouService.selectCheyouList(cheyou);
		if(cheyouhui.getNumber()<list.size()){
			return error("车友会会员人员已经超出会长设置范围！");
		}else{
			SysUser user = getSysUser();
			cheyou.setAddPersonID(user.getUserId());
			return toAjax(cheyouService.insertCheyou(cheyou));
		}

	}

	/**
	 * 修改车友会人员
	 */
	@GetMapping("/edit/{cheyouId}")
	public String edit(@PathVariable("cheyouId") Integer cheyouId, ModelMap mmap)
	{
		Cheyou cheyou = cheyouService.selectCheyouById(cheyouId);
		mmap.put("cheyous",cheyouhuiService.selectCheyouhuId(cheyouId));
		mmap.put("cheyou", cheyou);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存车友会人员
	 */
	@RequiresPermissions("system:cheyou:edit")
	@Log(title = "车友会人员", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Cheyou cheyou)
	{		
		return toAjax(cheyouService.updateCheyou(cheyou));
	}
	
	/**
	 * 删除车友会人员
	 */
	@RequiresPermissions("system:cheyou:remove")
	@Log(title = "车友会人员", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(cheyouService.deleteCheyouByIds(ids));
	}
	
}
